package com.bkhc.file.api;

import com.bkhc.common.annotation.ApiOperation;
import com.bkhc.common.annotation.ApiPackage;
import com.bkhc.common.annotation.AuditLog;
import com.bkhc.common.enu.OperationEnum;
import com.bkhc.common.util.response.ResultJson;
import com.bkhc.file.service.FileService;
import io.swagger.annotations.Api;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @author pcq
 * @date 2021/6/20 17:58
 */

@RestController
@RequestMapping
@Api("文件管理服务")
@ApiPackage(code="file-api",name="文件管理服务",description = "文件管理维护操作接口")
public class FileController {
    private static Log log = LogFactory.getLog(FileController.class);

    @Autowired
    private FileService fileService;

    @ApiOperation(code="uploadFile",name="",description = "上传文件",type = "common")
    @AuditLog(object="file",operation = "上传文件",type = OperationEnum.add)
    @RequestMapping(path = "/uploadFile",method = RequestMethod.POST)
    public ResultJson<?> uploadFile(@RequestParam("file") MultipartFile file){
        String fileId = fileService.uploadFile(file);
        Map<String,String> map = new HashMap<String,String>();
        map.put("fileId",fileId);
        ResultJson<Map<String,String>> resultJson = ResultJson.success();
        resultJson.setData(map);
        return resultJson;
    }

    @ApiOperation(code="uploadProductFile",name="",description = "上传产品技术文件",type = "common")
    @AuditLog(object="file",operation = "上传产品技术文件",type = OperationEnum.add)
    @RequestMapping(path = "/uploadProductFile/{productCode}",method = RequestMethod.POST)
    public ResultJson<?> uploadProductFile(@RequestParam("file") MultipartFile file,@PathVariable(name="productCode") String productCode){
        String fileId = fileService.uploadProductFile(file,productCode);
        Map<String,String> map = new HashMap<String,String>();
        map.put("fileId",fileId);
        ResultJson<Map<String,String>> resultJson = ResultJson.success();
        resultJson.setData(map);
        return resultJson;
    }

    @ApiOperation(code="uploadProductVedio",name="",description = "上传产品视频文件",type = "common")
    @AuditLog(object="file",operation = "上传产品视频文件",type = OperationEnum.add)
    @RequestMapping(path = "/uploadProductVedio/{productCode}",method = RequestMethod.POST)
    public ResultJson<?> uploadProductVedio(@RequestParam("file") MultipartFile file,@PathVariable(name="productCode") String productCode){
        String fileId = fileService.uploadProductVedio(file,productCode);
        Map<String,String> map = new HashMap<String,String>();
        map.put("fileId",fileId);
        ResultJson<Map<String,String>> resultJson = ResultJson.success();
        resultJson.setData(map);
        return resultJson;
    }

    @PostMapping("/uploadMultipleFiles")
    public List<ResultJson<?>> uploadMultipleFiles(@RequestParam("files") MultipartFile[] files) {
     return Arrays.stream(files)
                .map(this::uploadFile)
                .collect(Collectors.toList());
    }

    @ApiOperation(code="downloadFile",name="",description = "下载文件",type = "common")
    @AuditLog(object="file",operation = "下载文件",type = OperationEnum.add)
    @RequestMapping(path = "/downloadFile/{fileId}",method = RequestMethod.GET)
    public ResponseEntity<Resource> downloadFile(@PathVariable String fileId, HttpServletRequest request) throws Exception {
        Resource resource = fileService.downloadFile(fileId);
        String contentType = null;
        try {
            request.getServletContext().getMimeType(resource.getFile().getAbsolutePath());
        } catch (IOException e) {
            log.info("Could not determine file type.");
        }
        if(contentType == null) {
            contentType = "application/octet-stream";
        }
        return ResponseEntity.ok()
                .contentType(MediaType.parseMediaType(contentType))
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"")
                .body(resource);
    }

}
